package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.pojo.Sport;
import com.itheima.service.SportService;
import com.itheima.utils.POIUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/sport")
public class SportController {
    @Reference
    private SportService sportServie;


    /**
     * 上传文件/批量导入运动信息
     *
     * @param excelFile
     * @return
     */
    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile excelFile) {
        try {
            List<String[]> list = POIUtils.readExcel(excelFile);//使用POI解析表格数据
            List<Sport> data = new ArrayList<>();
            for (String[] strings : list) {
                String id = strings[0];
                System.out.println(id);

                String code = strings[1];
                System.out.println(code);

                String name = strings[2];
                System.out.println(name);

                String risk = strings[3];
                String age = strings[4];
                String sportTime = strings[5];
                String frequency = strings[6];
                String intensity = strings[7];
                Sport sport = new Sport(Integer.parseInt(id.trim()), code, name, Integer.parseInt(risk), age, sportTime, frequency, Integer.parseInt(intensity.trim()));
                data.add(sport);
            }
            sportServie.insertSport(data);
            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        } catch (IOException e) {
            e.printStackTrace();
            //文件解析失败
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }


    }


    /**
     * 分页查询运动库
     *
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return sportServie.findpage(queryPageBean);
    }

    /**
     * 新增运动库信息
     */
    @RequestMapping("/addSport")
    public Result addSport(@RequestBody Sport sport) {
        try {
            sportServie.addSport(sport);
            return new Result(true, MessageConstant.ADD_SPORT_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_SPORT_FAIL);
        }
    }

    /**
     * 根据id获取运动的信息
     *
     * @param id
     * @return
     */
    @GetMapping("/findSportById")
    public Result findSportById(Integer id) {

        try {
            Sport sport = sportServie.findSportById(id);
            return new Result(true, MessageConstant.GET_SPORT_SUCCESS, sport);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_SETMEAL_LIST_FAIL);
        }
    }


    /**
     * 编辑运动信息
     *
     * @param sport
     * @return
     */
    @RequestMapping("/editSport")
    public Result editSport(@RequestBody Sport sport) {
        try {
            sportServie.editSport(sport);
            return new Result(true, MessageConstant.EDIT_SPORT_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.EDIT_SPORT_FAIL);
        }
    }

    /**
     * 根据删除运动信息
     *
     * @param id
     * @return
     */
    @GetMapping("/deleteSportById")
    public Result deleteSportById(Integer id) {
        try {
            sportServie.deleteSportById(id);
            return new Result(true, MessageConstant.DELETE_SPORT_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_SPORT_FAIL);
        }
    }
}
